﻿// MIT License
//
// Copyright (c) 2021-present zuohuaijun, Daming Co.,Ltd and Contributors
//
// 电话/微信：18020030720 QQ群1：87333204 QQ群2：252381476

using Admin.NET.Application.Const;
using Admin.Net.InformationPlatform.Entity;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Admin.Net.InformationPlatform.Service.ProjectFile.Dto;

namespace Admin.Net.InformationPlatform.Service.EIPD;
/// <summary>
/// EIPD帆软报表工具类
/// </summary>
[ApiDescriptionSettings(groups: "EIPDFineReport", Name = "帆软报表工具类", Tag = "帆软报表", Order = 200)]
[Route("EIPD/")]
public class EIPDFineReportService : IDynamicApiController, ITransient
{

    /// <summary>
    /// 单例数据链接
    /// </summary>
    ISqlSugarClient db;
    /// <summary>
    /// T100数据库
    /// </summary>
    private SqlSugarProvider _T100Db;

    private SqlSugarProvider _Dilon;
    //OA数据库
    private SqlSugarProvider _Ecology;

    /// <summary>
    /// 用户信息
    /// </summary>
    TokenUser _User;
    /// <summary>
    /// SOP工程文件管理
    /// </summary>
    /// <param name="db"></param>
    public EIPDFineReportService(ISqlSugarClient db)
    {
        this.db = db;
        this._T100Db = this.db.AsTenant().GetConnection(T100Area.ConfigId);

        this._Dilon = this.db.AsTenant().GetConnection(Dilon.ConfigId);
        this._Ecology = this.db.AsTenant().GetConnection(Ecology.ConfigId);
        // 获取 `Jwt` 存储的信息 企业编号与据点信息
        _User = Util.SjCommonUtil.GetT100UserTokenInfo(_T100Db, _Dilon);
        // _sysPhoneCode = sysPhoneCode;

    }

    /// <summary>
    /// 根据流程名称获取流程信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("/GetWorkflowsList")]
    [AllowAnonymous]
    public List<WorkflowMessage> GetWorkflowsList(string input)
    {



        List<WorkflowMessage> list = _Ecology.Ado.SqlQuery<WorkflowMessage>($@"select * from (
                    SELECT
                        A.REQUESTID as REQUESTID
                        , BASE.WORKFLOWNAME  as WORKFLOWNAME
                        , A.REQUESTNAME as REQUESTNAME
                        , NODE.NODENAME as NODENAME
                        , (CASE WHEN A.CREATER=1 THEN '系统管理员' ELSE RES.LASTNAME END) as CREATENAME
                        , A.CREATEDATE + ' ' + A.CREATETIME as CREATETIME
                        , CR.LASTNAME as LASTNAME
                        , CR.RECEIVEDATE + ' ' + CR.RECEIVETIME as RECEIVETIME 
                        , A.STATUS as STATUS
                        , (CASE WHEN A.CURRENTNODETYPE=0 THEN '创建节点' WHEN A.CURRENTNODETYPE=1 THEN '审批节点' WHEN A.CURRENTNODETYPE=2 THEN '处理节点' ELSE '其他节点' END) as CURRENTNODETYPE
                    FROM WORKFLOW_REQUESTBASE A
                    INNER JOIN WORKFLOW_NODEBASE NODE ON NODE.ID=A.CURRENTNODEID
                    LEFT JOIN HRMRESOURCE RES ON RES.ID=A.CREATER
                    INNER JOIN WORKFLOW_BASE BASE ON BASE.ID=A.WORKFLOWID and  LEFT(BASE.WORKFLOWNAME, 3) = 'IPD'
                    INNER JOIN (SELECT REQUESTID, R1.LASTNAME, RECEIVEDATE, RECEIVETIME FROM WORKFLOW_CURRENTOPERATOR C1
                    INNER JOIN HRMRESOURCE R1 ON R1.ID=C1.USERID
                    WHERE C1.ID IN (SELECT MAX(ID) FROM WORKFLOW_CURRENTOPERATOR GROUP BY REQUESTID)) CR ON CR.REQUESTID=A.REQUESTID
                    WHERE CURRENTNODETYPE != 3
                    AND (
                        (
                            EXISTS (
                                SELECT 1
                                FROM WORKFLOW_BASE WB
                                WHERE A.WORKFLOWID = WB.ID
                                AND WB.ISVALID IN ('0', '1', '2', '3')
                                AND WB.ID IN (
                                    SELECT ID
                                    FROM WORKFLOW_BASE WORKBASE, WORKFLOW_VERSIONINFO WORKVERSION
                                    WHERE WORKBASE.ID = WORKVERSION.WFID
                                    AND WFVERSIONID IN (
                                        SELECT WORKFLOWID FROM WORKFLOW_MONITOR_DETAIL WHERE INFOID = 0
                                    )
                                )
                            )
                        )
                    )

                    AND A.DELETED < 1) a
                    where a.WORKFLOWNAME = '{input}' 
                    ORDER BY A.REQUESTID DESC"
                  ).ToList();



        return list;
    }

}